import cgi
import datetime
import os
import lib
import logging
import time
import inspect
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
from google.appengine.api import datastore_errors
from google.appengine.ext.webapp import template
from lib import llhandler
from lib.workflowManager import WorkflowManager
from model.models import *
class bulkdelete(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        kind_names =[]
        for name in dir(model.models):
	  obj = getattr(model.models, name)
	  if inspect.isclass(obj):
	    kind_names.append(name)
	    
        
	    
	while len(kind_names)>0:
	    kind_name = kind_names.pop()
	    try:
	      self.response.out.write('Deleting kind ' + kind_name + ' \n')
	    #try:
	      #while True:
	      
	      q = db.GqlQuery("SELECT __key__ FROM " + kind_name)
		#assert q.count()
	      
	      db.delete(q.fetch(200))
	      time.sleep(0.5)
	    except Exception, e:
	      self.response.out.write(repr(e)+'\n')
    
	  
def main():
  logging.getLogger().setLevel(logging.DEBUG)
  application = webapp.WSGIApplication([('/admin/restart/bulkdelete/.*',bulkdelete),
					],
                                       debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()